package com.source.elasticsearch.controller;

import com.source.elasticsearch.bean.Content;
import com.source.elasticsearch.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @Author wengqy
 * @Date 2021/1/26
 */
@Controller
public class IndexController {

    @Autowired
    private ContentService contentService;

   /* @GetMapping("/parse/{keyword}")
    public Boolean parse(@PathVariable("keyword") String keyword) throws Exception {
        return contentService.parseContent(keyword);
    }
*/
    //http://localhost:8080/search/java/1/10
/*
    @GetMapping("/search/{keyword}/{pageNo}/{pageSize}")
    public List<Map<String, Object>> search(@PathVariable("keyword") String keyword, @PathVariable("pageNo") int pageNo, @PathVariable("pageSize") int pageSize) throws Exception {
        return contentService.searchContentPage(keyword, pageNo, pageSize);
    }
*/

/*    @ResponseBody
    @GetMapping("/search/{keyword}/{pageNo}/{pageSize}")
    public List<Map<String, Object>> search(@PathVariable("keyword") String keyword, @PathVariable("pageNo") int pageNo, @PathVariable("pageSize") int pageSize) throws Exception {
        return contentService.searchContentPage(keyword, pageNo, pageSize);
    }*/

    @GetMapping("/search/{keyword}/{pageNo}/{pageSize}")
    public String search(@PathVariable("keyword") String keyword, @PathVariable("pageNo") int pageNo, @PathVariable("pageSize") int pageSize, Model model) throws Exception {
        List<Map<String, Object>> list = contentService.searchContentPage(keyword, pageNo, pageSize);
        List<Content> conList =  new ArrayList<Content>();
        Content content = null;
        for (Map<String, Object> map : list) {
            String title = (String)map.get("title");
            String price = (String)map.get("price");
            String img = (String)map.get("img");
            content = new Content();
            content.setImg(img);
            content.setPrice(price);
            content.setTitle(title);
            conList.add(content);
        }
        model.addAttribute("conList",conList);


        return "index";
    }
    @GetMapping("/index")
    public String index( )     {
        return  "index";
    }
}
